package com.ruoyi.project.management.replace.controller;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.management.delay.mapper.ManagementTaskMapper;
import com.ruoyi.project.management.tranfer.domain.TaskTransfer;
import com.ruoyi.project.management.tranfer.mapper.ManagementTransferMapper;
import com.ruoyi.project.management.tranfer.service.ManagementTransferService;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/management/replace")
public class ManagementReplaceController extends BaseController {

    @Autowired
    private ManagementTransferService managementTransferService;

    @Autowired
    private ManagementTransferMapper managementTransferMapper;

    @Autowired
    private ManagementTaskMapper managementTaskMapper;

    private String prefix = "management/replace";


    /**
     * 跳转到任务转移
     * @return
     */
    @GetMapping()
    public String task()
    {
        return prefix + "/replace";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TaskTransfer taskTransfer)
    {
        startPage();
        List<TaskTransfer> list = managementTransferService.selectAll(taskTransfer);
        return getDataTable(list);
    }

    /**
     * 审核任务转移
     */
    @GetMapping("/edit/{transfer_id}")
    public String edit(@PathVariable("transfer_id") Integer transfer_id, ModelMap mmap)
    {   //根据转移任务id查看任务信息
        TaskTransfer taskTransfer = managementTransferService.selectById(transfer_id);
        if(taskTransfer.getSuccessor() != null){
            //根据查到的继任者id查询继任者姓名
            TaskTransfer taskTransfer1 = managementTransferMapper.selectBySuccessor(taskTransfer.getSuccessor());
            System.out.println(taskTransfer1.getSucce_name());
            //把查询到的继任者姓名存入对象
            taskTransfer.setSucce_name(taskTransfer1.getSucce_name());
        }
        mmap.put("replace", taskTransfer);
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TaskTransfer taskTransfer)
    {
        //生成审核时间并注入对象
        taskTransfer.setAudit_time(new Date());

        if(taskTransfer.getPass() == 1 && taskTransfer.getSuccessor() != null){
            System.out.println("+++++++++++++++++++Yes++++++++++++++++++");
            //当审核通过并且存在接替人时修改接替者为任务执行人
            managementTaskMapper.updateByExecutor(taskTransfer.getSuccessor(),taskTransfer.getTask_id());
        }
        ShiroUtils.clearCachedAuthorizationInfo();

        return toAjax(managementTransferService.update(taskTransfer));
    }


    /**
     * 根据转移任务id查看任务信息
     * @param
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{transfer_id}")
    public String show(@PathVariable("transfer_id") Integer transfer_id, ModelMap mmap)
    {
        TaskTransfer taskTransfer = managementTransferService.selectById(transfer_id);
        if(taskTransfer.getSuccessor() != null){
            //根据查到的继任者id查询继任者姓名
            TaskTransfer transfer1 = managementTransferMapper.selectBySuccessor(taskTransfer.getSuccessor());
            //把查询到的继任者姓名存入对象
            taskTransfer.setSucce_name(transfer1.getSucce_name());
        }
        mmap.put("replace", taskTransfer);
        return prefix + "/show";
    }




}
